'''
Created on 22 sept. 2010

@author: Dimitri
'''

from idho.listener import Win32FileListener
from idho.synchronizer import FileSynchronizer
import ConfigParser
import os
import getpass
import sys

defaults = {
            'localfolder': os.path.expanduser(r'~\workspace'),
            'remotefolder': '',
            'remotehost': '',
            'user': getpass.getuser(),
            'rsyncoptions': '',
            'delay': '0.5',
            'ignore': '.metadata|.svn'
            }

configpath = os.path.expanduser('~/idhofs.cfg')
#/cygdrive/f/
config = ConfigParser.ConfigParser(defaults)
if os.path.exists(configpath):
    config.read(configpath)
else:
    with open(configpath, 'w+') as file:
        config.write(file)

sections = config.sections()

if not sections:
    print 'No section was defined in the configuration file (' + configpath + ')'
    sys.stdin.readline()
else:
    for section in sections:
        configuration = dict(config.items(section))
        synchronizer = FileSynchronizer(configuration)
        #Trigger first rsync
        synchronizer._recursive = True
        synchronizer._synchronize()
        listener = Win32FileListener(synchronizer, configuration)
        listener.start()
